local redis = require "resty.redis"  --依赖包

 ngx.shared.DICT:set("s","123")

local s= ngx.shared.DICT:get("s")
ngx.say("从shared中拿值"..s);

ngx.say("尝试塞入table"..s);

ngx.shared.DICT:set("mytable", { a=1,b=2 })
local mytable= ngx.shared.DICT:get("mytable")
ngx.say("尝试获取table"..mytable.a);
local red= ngx.shared.DICT:get("red")
if not red then
    ngx.say("redis不存在缓存中")
    red = redis:new()   ---创建客户端
    red:set_timeout(6000) -- 1 sec
    local ok,err=red.connect(red,'192.168.213.26',6379)  --连接地址和端口
    if not ok then
        ngx.say("failed to connect:",err)  ---如果连接错误打印
    return
    end


    ok, err = red:auth("123456")   --输入redis 密码
    if not ok then
        ngx.say("failed to connect: ", err)   --密码验证失败
        return
    end




    ngx.shared.DICT:set("redis",red);
else
    ngx.say("redis存在缓存中")
end


ok, err = red:set("dog", "an animal")    --设置key dog
if not ok then
    ngx.say("failed to set dog: ", err)   --
    return
end

ngx.say("set result: ", ok)

local res, err = red:get("dog")   --取出刚才设置的值
if not res then
    ngx.say("failed to get dog: ", err)
    return
end
------使用
if res == ngx.null then
    ngx.say("dog not found.")
    return
end

ngx.say("dog: ", res)   ---打印redis 中dog的值

-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(10000, 100)
if not ok then
    ngx.say("failed to set keepalive: ", err)
    return
end

-- or just close the connection right away:
-- local ok, err = red:close()
-- if not ok then
--     ngx.say("failed to close: ", err)
--     return
-- end